<template>
  <div class="userInfo">
    <el-card>
      <!-- 头部区域 -->
      <header-search>
        <template #search>
          <span>用户名</span>
          <el-input
            placeholder="请输入用户名称"
            style="width: 220px"
            size="small"
          />
          <span>手机号</span>
          <el-input
            placeholder="请输入手机号"
            style="width: 220px"
            size="small"
          />
          <span>性别</span>
          <el-select
            v-model="queryData.sex"
            placeholder="请输入手机号"
            style="width: 220px"
            size="small"
          >
            <el-option
              v-for="item in sexList"
              :key="item.id"
              :label="item.sexname"
              :value="item.id"
            />
          </el-select>
          <el-button
            icon="el-icon-search"
            type="primary"
            size="small"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="small">重置</el-button>
        </template>

        <template #addContent>
          <el-button
            icon="el-icon-plus"
            type="primary"
            size="small"
            @click="addUser"
          >新增</el-button>
          <!-- 导出组件 -->
          <export-upload type="user" />
        </template>
      </header-search>

      <!-- 列表 -->
      <el-table :data="tableData" border style="width: 100%" class="usertable">
        <el-table-column
          fixed
          label="#"
          width="80px"
          type="index"
          :index="indexMethod"
        />
        <el-table-column fixed prop="id" label="用户ID" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="icon" label="用户头像">
          <template #default="{ row }">
            <img
              :src="'http://124.223.14.236:8060/' + row.icon"
              alt=""
              style="width: 120px"
            >
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别">
          <template #default="{ row }">
            {{ handleSex(row.sex) }}
          </template>
        </el-table-column>
        <el-table-column prop="birthday" label="出生年月" />
        <el-table-column fixed="right" label="操作">
          <template #default="{row}">
            <el-button icon="el-icon-edit" type="primary" size="small" @click="editUserInfo(row.id)" />
            <el-button icon="el-icon-delete" type="danger" size="small" @click="delUserInfo(row.id)" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        :current-page="queryData.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryData.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 弹框 -->
    <AddUserDialog ref="addUserDialog" @updata="initData" />
  </div>
</template>
<script>
import { getUserListApi, delUserInfoApi } from '@/api/vip/user'
import AddUserDialog from './components/user-dialog.vue'
export default {
  name: 'UserInfo',
  components: {
    AddUserDialog
  },
  data() {
    return {
      // 表格数据
      tableData: [],
      // 请求参数
      queryData: {
        sex: '',
        username: '',
        mobile: '',
        page: 1,
        limit: 10
      },
      // 总数
      total: 0,
      // 性别
      sexList: [
        { sexname: '男', id: 0 },
        { sexname: '女', id: 1 }
      ]
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 数据初始化
    async initData() {
      const { data: res } = await getUserListApi(this.queryData)
      this.total = res.count
      this.tableData = res.data
    },
    // 处列表理序号
    indexMethod(index) {
      return (this.queryData.page - 1) * this.queryData.limit + index + 1
    },
    // 分页器 => 每页显示条数
    handleSizeChange(val) {
      this.queryData.limit = val
      this.queryData.page = 1
      this.initData()
    },
    // 分页器 => 当前页
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 处理性别
    handleSex(sex) {
      if (sex === 0) {
        return '女'
      } else {
        return '男'
      }
    },
    // 删除
    async delUserInfo(id) {
      try {
        await this.$confirm('确认删除吗？', '提示')
      } catch (error) {
        return this.$message('取消')
      }
      // 处理删除页边界
      if (this.tableData.length === 1 && this.queryData.page > 1) {
        this.queryData.page--
      }
      await delUserInfoApi(id)
      this.$message.success('删除成功')
      this.initData()
    },
    // 添加
    addUser() {
      this.$refs.addUserDialog.showDialog = true
    },
    // 编辑回显
    editUserInfo(id) {
      this.$refs.addUserDialog.getUserInfoDetail(id)
    }
  }
}
</script>
<style lang="scss" scoped>
.userInfo {
  padding: 20px 20px 0 20px;
  .el-pagination {
    margin-top: 20px;
    float: right;
  }
  .usertable {
    .el-button {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      padding: 0;
    }
  }
  .el-select {
    margin-right: 30px;
  }
}
</style>
